package com.broada.apm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.broada.apm.ajax.PerformanceResult;
import com.broada.apm.base.BaseService;
import com.broada.apm.entity.Service;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.model.PerformanceView;
import com.broada.apm.service.V1PerformanceService;

public class V1PerformanceServiceImpl extends BaseService implements V1PerformanceService {

	private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	
	@Override
	public PerformanceResult queryPerformanceByDate(String appId, String appName, String agentId, String ip, String port, String txId,
			String typeName, String channelName, Integer last, String field) {
		PerformanceResult result = new PerformanceResult();
		long date = System.currentTimeMillis();
		List<String> applicationIds = new ArrayList<String>();

		// 应用系统
		if (StringUtils.isNotBlank(appName) && StringUtils.isBlank(appId)) {
			com.broada.apm.model.ApplicationSystem system = appLogic.getApplicationByName(appName);
			if (system == null) {
				result.setFailMessage("应用系统不存在");
				return result;
			}
			appId = system.getId();
			applicationIds = appLogic.queryApplicationIdsBySystem(appId);
		}
		if (StringUtils.isBlank(appId)) {
			result.setFailMessage("参数错误");
			return result;
		}
		// 应用节点
		if ((StringUtils.isNotBlank(ip) && StringUtils.isBlank(port))
				|| (StringUtils.isNotBlank(port) && StringUtils.isBlank(ip))) {
			result.setFailMessage("参数错误");
			return result;
		} else if (StringUtils.isNotBlank(ip) && StringUtils.isNotBlank(port)) {
			Service service = serverLogic.getAppServiceByIpAndPort(appId, ip, Integer.parseInt(port));
			if (service == null) {
				result.setFailMessage("节点不存在");
				return result;
			}
			agentId = service.getId();
		}
		if (StringUtils.isNotBlank(typeName) && StringUtils.isBlank(agentId)) {
			result.setFailMessage("参数错误");
			return result;
		}

		long startDate = date - 5 * 60 * 1000;
		if (last != null) {
			startDate = date - last * 60 * 1000 - 10 * 60 * 1000;
		}
		List<PerformanceView> perfs = perfLogic.queryPerformance(applicationIds, agentId, txId, typeName, channelName, startDate, date,
				last, field);
		result.setPerformancesResult(perfs);
		return result;
	}

	@Override
	public PerformanceResult queryBusinessTotal(String appId, String appName) {
		PerformanceResult result = new PerformanceResult();

		// 应用系统
		com.broada.apm.model.ApplicationSystem applicaiton = new com.broada.apm.model.ApplicationSystem();
		List<String> applicationIds = new ArrayList<String>();
		if (StringUtils.isNotBlank(appName) && StringUtils.isBlank(appId)) {
			applicaiton = appLogic.getApplicationByName(appName);
			if (applicaiton == null) {
				result.setFailMessage("应用系统不存在");
				return result;
			}
			appId = applicaiton.getId();
			applicationIds = appLogic.queryApplicationIdsBySystem(appId);
			if (applicationIds.isEmpty()) {
				result.setFailMessage("应用系统中不存在任何节点");
				return result;
			}
		}
		if (StringUtils.isBlank(appId)) {
			result.setFailMessage("参数错误");
			return result;
		}

		Calendar today = Calendar.getInstance();
		today.set(Calendar.HOUR_OF_DAY, 0);
		today.set(Calendar.MINUTE, 0);
		today.set(Calendar.SECOND, 0);
		today.set(Calendar.MILLISECOND, 0);

		Calendar nextDay = Calendar.getInstance();
		nextDay.set(Calendar.HOUR_OF_DAY, 0);
		nextDay.set(Calendar.MINUTE, 0);
		nextDay.set(Calendar.SECOND, 0);
		nextDay.set(Calendar.MILLISECOND, 0);
		nextDay.add(Calendar.DAY_OF_MONTH, 1);

		List<PerformanceView> perfs = new ArrayList<PerformanceView>();
		try {
			perfs = perfLogic.queryThisDayData(applicaiton, applicationIds, today.getTime().getTime(), nextDay.getTime().getTime());
		} catch (ApmDataIndicatorException e) {
			result.setFailMessage("查询交易系统当天交易总量失败");
		}
		result.setPerformancesResult(perfs);
		return result;
	}

	@Override
	public PerformanceResult queryOneHourAlert(String appId, String appName) {
		PerformanceResult result = new PerformanceResult();
		long endTime = System.currentTimeMillis();

		// 应用系统
		com.broada.apm.model.ApplicationSystem applicaiton = new com.broada.apm.model.ApplicationSystem();
		if (StringUtils.isNotBlank(appName) && StringUtils.isBlank(appId)) {
			applicaiton = appLogic.getApplicationByName(appName);
			if (applicaiton == null) {
				result.setFailMessage("应用系统不存在");
				return result;
			}
			appId = applicaiton.getId();
		}
		if (StringUtils.isBlank(appId)) {
			result.setFailMessage("参数错误");
			return result;
		}
		
		long startTime = endTime / 600000 * 600000;
		long currentTime = endTime / 60000 * 60000;
		List<PerformanceView> list = new ArrayList<PerformanceView>();
		for (int i=5; i>=0; i--) {
			Integer count = alertLogic.queryAppAlertCount(appId, new Date(startTime - (i + 1) * 10 * 60 * 1000), new Date(startTime - i * 10 * 60 * 1000));
			PerformanceView perf = new PerformanceView();
			perf.put("alerts", count);
			perf.put("time", sdf.format(new Date(startTime - i * 10 * 60 * 1000)));
			list.add(perf);
		}
		// 当前时间的告警数
		PerformanceView currentPerf = new PerformanceView();
		currentPerf.put("alerts", alertLogic.queryAppAlertCount(appId, new Date(startTime), new Date(currentTime)));
		currentPerf.put("time", sdf.format(new Date(currentTime)));
		list.add(currentPerf);
		
		result.setPerformancesResult(list);
		return result;
	}

}
